package com.example.infowall02.utils;

import org.springframework.stereotype.Component;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Component
public class MD5Util {
    public static String md5(String input) {
        try {
            // 获取 MD5 算法的 MessageDigest 实例
            MessageDigest digest = MessageDigest.getInstance("MD5");
            // 更新要计算的数据
            digest.update(input.getBytes());
            // 进行哈希计算，得到字节数组
            byte[] messageDigest = digest.digest();
            // 将字节数组转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xFF & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            // 处理算法不存在的异常
            e.printStackTrace();
            return null;
        }
    }
}